package com.dora.algorithm.leetcode;

import java.util.Arrays;
import java.util.Hashtable;

/**
 * 两数相加
 * <p>[2,7,11,15]  target = 9</p>
 *
 * @author hanchen
 * @date 2024/02/29
 */
public class Q1_TwoSum {

    public static void main(String[] args) {
        int[] array = {3,2,4};
        int[] result = twoSumCommon(array, 6);
        System.out.println(Arrays.toString(result));
    }


    public static int[] twoSumCommon(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target && i!=j) {
                    return new int[]{i, j};
                }
            }
        }
        return null;
    }

    public static int[] twoSumAdvanced(int[] nums, int target) {
        Hashtable<Integer, Integer> map = new Hashtable<>();
        for (int i = 0; i < nums.length; i++) {
            int rest = target - nums[i];
            if (map.containsKey(rest)) {
                return new int[]{map.get(rest),i};
            }
            map.put(nums[i], i);
        }
        return null;
    }
}
